#include "searchdeviceitem.h"

#include <QEvent>
#include <QDebug>

SearchDeviceItem::SearchDeviceItem(QPushButton *parent) : QPushButton(parent)
{
    initUI();
}

SearchDeviceItem::~SearchDeviceItem()
{
}

void SearchDeviceItem::initUI()
{
    this->installEventFilter(this);
    QHBoxLayout *mainHLayout = new QHBoxLayout();

    QVBoxLayout *labelVLayout = new QVBoxLayout();
    //项按钮
    m_itemIconLabel = new QLabel(this);
    m_itemIconLabel->setFixedSize(48, 48);
    //用户名
    m_itemNameLabel = new QLabel(this);
    //手机型号
//    m_itemModelLabel = new QLabel(this);
//    m_itemModelLabel->setStyleSheet("color:#8F9399;font-size:12px");

    labelVLayout->addStretch();
    labelVLayout->addWidget(m_itemNameLabel);
//    labelVLayout->addWidget(m_itemModelLabel);
    labelVLayout->addStretch();

    mainHLayout->addSpacing(16);
    mainHLayout->addWidget(m_itemIconLabel);
    mainHLayout->addSpacing(8);
    mainHLayout->addLayout(labelVLayout);
    mainHLayout->addStretch();
    mainHLayout->setSpacing(0);
    mainHLayout->setMargin(0);
    this->setLayout(mainHLayout);

    QPalette palette = this->palette();
    palette.setColor(QPalette::Button, QColor("#FFFFFF"));
    this->setPalette(palette);

    this->setProperty("useButtonPalette", true);
}

void SearchDeviceItem::setItemIcon(QString path)
{
    m_itemIconLabel->setPixmap(QIcon(path).pixmap(48, 48));
}

void SearchDeviceItem::setItemName(QString name)
{
    m_name = name;
    m_itemNameLabel->setText(name);
}

void SearchDeviceItem::setItemDeviceInfo(KMobileCommon::DiscoveryDeviceInfo deviceInfo)
{
    m_deviceInfo = deviceInfo;
    // m_itemModelLabel->setText(deviceInfo.deviceMoudle);
}

void SearchDeviceItem::setItemIP(QString ip)
{
    m_itemIP = ip;
}

QString SearchDeviceItem::getItemName()
{
    return m_name;
}

KMobileCommon::DiscoveryDeviceInfo SearchDeviceItem::getItemDeviceInfo()
{
    return m_deviceInfo;
}

QString SearchDeviceItem::getItemIp()
{
    return m_itemIP;
}

bool SearchDeviceItem::eventFilter(QObject *watch, QEvent *event)
{
    if (watch == this) {
         if (event->type() == QEvent::Enter) {
             QPalette palette = this->palette();
             palette.setColor(QPalette::Button, QColor("#E6F1FE"));
             this->setPalette(palette);
         } else if (event->type() == QEvent::Leave) {
             QPalette palette = this->palette();
             palette.setColor(QPalette::Button, QColor("#FFFFFF"));
             this->setPalette(palette);
         }
     }
     return QObject::eventFilter(watch, event);
}
